package com.tj.collection.db.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tj.collection.db.dao.ChatterConfigDao;
import com.tj.collection.db.dao.RptChatterDataPreDayDao;
import com.tj.collection.db.dao.RptChatterOnlinePreHourDao;
import com.tj.collection.db.dao.RptModelDataPreDayDao;
import com.tj.collection.db.domain.ChatterOnlineDto;
import com.tj.collection.db.domain.RptChatterDataPreDayDto;
import com.tj.collection.db.domain.RptModelDataPreDayDto;
import com.tj.collection.db.entity.ChatterConfigEntity;
import com.tj.collection.db.entity.RptChatterDataPreDayEntity;
import com.tj.collection.db.entity.RptChatterOnlinePreHourEntity;
import com.tj.collection.db.entity.RptModelDataPreDayEntity;
import com.tj.collection.db.service.QualityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 质检service处理类
 */
@Service("qualityService")
public class QualityServiceImpl implements QualityService {

    @Resource
    RptModelDataPreDayDao modelDataPreDayDao;
    @Resource
    RptChatterDataPreDayDao chatterDataPreDayDao;
    @Resource
    RptChatterOnlinePreHourDao chatterOnlinePreHourDao;
    @Resource
    ChatterConfigDao chatterConfigDao;
    /**
     * model每日情况统计
     */
    @Override
    public List<RptModelDataPreDayDto> listModelData(String chatterCode, Date startDate, Date endDate, Page page) {
        final List<RptModelDataPreDayEntity> modelDatas = modelDataPreDayDao.listModelData(chatterCode, startDate, endDate, page);
        if (CollectionUtils.isEmpty(modelDatas)) {
            return Lists.newArrayList();
        }
        return modelDatas.stream().map(RptModelDataPreDayDto::toDto).collect(Collectors.toList());
    }

    @Override
    public Map<String, RptChatterDataPreDayDto> listChatterData(List<String> chatterCodes, Date startDate, Date endDate) {

        // 获取chatter数据
        final List<RptChatterDataPreDayEntity> chatterDatas = chatterDataPreDayDao.listChatterData(chatterCodes, startDate, endDate);
        if (CollectionUtils.isEmpty(chatterDatas)) {
            return Maps.newHashMap();
        }

        // 获取chatter的用户在线配置
        final List<ChatterConfigEntity> allChatterConfigs = chatterConfigDao.listAllChatterConfigs();

        // 获取chatter当前时段在线用户数据
        final List<RptChatterOnlinePreHourEntity> onlinePreHourEntities = chatterOnlinePreHourDao.listChatterOnlinePreHour();
//        final Integer onlineUserNumToday = chatterOnlinePreHourDao.getOnlineTotal(startDate, endDate);
        final List<RptChatterOnlinePreHourEntity> onlinePreHourTotalList = chatterOnlinePreHourDao.getOnlineUserTotal(startDate,endDate);

        final Map<String,RptChatterOnlinePreHourEntity> onlinePreHourTotalMap = onlinePreHourTotalList.stream().collect(Collectors.toMap(RptChatterOnlinePreHourEntity::getChatterUserCode, Function.identity()));

        return chatterDatas.stream().map(o -> RptChatterDataPreDayDto.toDto(o,  allChatterConfigs, onlinePreHourEntities,onlinePreHourTotalMap)).collect(Collectors.toMap(RptChatterDataPreDayDto::getChatterUserCode,Function.identity()));
    }

    @Override
    public List<ChatterOnlineDto> listChatterOnline(String chatterCode, Date startDate, Date endDate) {
        final List<RptChatterOnlinePreHourEntity> chatterOnlineUsers = chatterOnlinePreHourDao.listChatterOnline(chatterCode, Integer.valueOf(DateUtil.format(startDate, "yyyyMMdd")), Integer.valueOf(DateUtil.format(endDate, "yyyyMMdd")));
        if (CollectionUtils.isEmpty(chatterOnlineUsers)) {
            return Lists.newArrayList();
        }
        return chatterOnlineUsers.stream().map(ChatterOnlineDto::toDto).collect(Collectors.toList());

    }
}
